package com.example.petadopt.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.entity.Pet;
import com.gzone.university.utils.GlideUtils;

import java.util.List;

public class PetDiscoverAdapter extends RecyclerView.Adapter<PetDiscoverAdapter.ViewHolder> {

    private List<Pet> petList;
    private OnItemClickListener onItemClickListener;

    public PetDiscoverAdapter(List<Pet> petList) {
        this.petList = petList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pet_discover, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pet pet = petList.get(position);

        // 设置宠物图片
        if (pet.getImage() != null && !pet.getImage().isEmpty()) {
            GlideUtils.load(holder.ivPetImage, pet.getImage());
        }

        // 设置宠物名称
        holder.tvPetName.setText(pet.getName());

        // 设置宠物年龄
        holder.tvPetAge.setText(pet.getAge() + "岁");

        // 设置宠物品种和其他信息
        String breedInfo = pet.getBreed();
        if (pet.getIsAdopted() != null && pet.getIsAdopted()) {
            breedInfo += " · 已被领养";
        } else {
            if (pet.getGender() != null) {
                breedInfo += " · " + pet.getGender();
            }
        }
        holder.tvPetBreed.setText(breedInfo);

        // 设置宠物地址
        holder.tvPetAddress.setText(pet.getAddress());

        // 点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(pet, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return petList == null ? 0 : petList.size();
    }

    public void updateData(List<Pet> newPetList) {
        this.petList = newPetList;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPetImage;
        TextView tvPetName;
        TextView tvPetAge;
        TextView tvPetBreed;
        TextView tvPetAddress;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetImage = itemView.findViewById(R.id.iv_pet_image);
            tvPetName = itemView.findViewById(R.id.tv_pet_name);
            tvPetAge = itemView.findViewById(R.id.tv_pet_age);
            tvPetBreed = itemView.findViewById(R.id.tv_pet_breed);
            tvPetAddress = itemView.findViewById(R.id.tv_pet_address);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Pet pet, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }
} 